﻿using hoemwork;
using hoemwork._17bangTableAdapters;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static hoemwork._17bang;
using Microsoft.EntityFrameworkCore.Storage;

namespace hoemwork
{

    public class Program
    {


        static void Main(string[] args)
        {

            //用事务实现帮帮币出售的过程
            //卖方帮帮币足够，扣减数额后成功提交。
            //卖房帮帮币不够，事务回滚，买卖双方帮帮币不变。

            SqlDbContext context = new SqlDbContext();
            //using (IDbContextTransaction transaction = context.Database.BeginTransaction())
            //{

            //    try
            //    {


            //        context.SaveChanges();
            //        transaction.Commit();
            //    }
            //    catch (Exception)
            //    {
            //        transaction.Rollback();
            //        throw;
            //    }

            //}
            //BangMoney ls = new BangMoney { BMoney = 100  };
            //BangMoney fg = new BangMoney { BMoney = 100 };
            //context.AddRange(ls, fg);
            //context.SaveChanges();






            //Student lzb = context.Find<Student>(1);
            //context.Entry(lzb).Collection(s => s.Teacher).Load();
            //lzb.Teacher = null;
            //Teacher fg = context.Find<Teacher>(1);   
            //Classroom c1 = context.Find<Classroom>(1);
            //c1.Students.RemoveAt(1);
            //context.SaveChanges();
            //IList<Student> students =
            //    context.Students.Where(s => s.Id < 3).ToList();
            ////context.Find<Classroom>(2).Students = students;
            //context.Find<Teacher>(2).Students = students; 
            //context.SaveChanges();












            //Teacher t2 = context.Teachers
            //    .Include(t => t.Students)
            //    .Where(t => t.Id == 2)
            //    .SingleOrDefault();
            //context.Remove(t2);
            //context.SaveChanges(); 

            //IList<Student> students =
            //    context.Students.Where(s => s.Id < 3).ToList();//添加的数据
            //Teacher t2 = context.Find<Teacher>(2);//删除这个老师的学生
            //t2.Students.Clear();//删除
            //t2.Students = students;//query num add
            //context.SaveChanges();



            //Bed lw = context.Find<Bed>(2);
            //context.Entry(lw).Reference(s => s.Owner).Load();
            //context.Remove(lw);
            //context.SaveChanges();













            //Student zl = context.Students.Where(s => s.Name == "zl").SingleOrDefault();
            //foreach (var item in zl.Teacher)
            //{
            //    Console.WriteLine(item.Name);
            //}
            //Classroom c1 = context.Find<Classroom>(1);
            //foreach (var item in c1.Students)
            //{
            //    Console.WriteLine(item.Name);
            //}


            //  Console.WriteLine();

            //Classroom c1 = context.Classrooms
            //    .Include(c => c.Students
            //    .AsQueryable().Where(s => s.Name.Contains("l"))
            //    )
            //    .Where(c => c.Id == 2).SingleOrDefault();

            ////context.Entry(c1)
            ////    .Collection(c => c.Students)
            ////    .Query().Where(s => s.Name.Contains("l")).Load();

            //foreach (var item in c1.Students //LazyLoad的问题
            //                                 //.AsQueryable() //无法解决全部取出来的问题
            //                                         )                        //.Where(s=>s.Name.Contains('l'))

            //{
            //    Console.WriteLine(item.Name);
            //}













            //Student zl = context.Students.Where(s => s.Name == "zl").SingleOrDefault();

            //// /////zl.SleepIn null
            //// ///// EF生成BedProxy:Bed=>只有一个Id的值是现成的
            //// ///// 其他的都是default，直到使用的时候，由EF自动加载（到数据库查询）

            //Console.WriteLine(zl.SleepIn.Location);
            //Console.WriteLine(zl.StudyIn.Students.Count);


            //Bed bed = context.Find<Bed>(1);
            //Teacher fg = context.Teachers
            //         .Where(t => t.Name == "fg")
            //    //.Include(t => t.Students)
            //    //.ThenInclude(s => s.SleepIn)
            //    .SingleOrDefault();




            //context.Entry(fg).
            //Collection(f => f.Students)
            //.Load();
            // context.Entry(zl)
            //.Property("EnrollDate").CurrentValue = DateTime.Now;
            //.Reference(z => z.SleepIn)
            //.Load();
            //  context.SaveChanges();
            //Console.WriteLine();
            //Console.WriteLine(query.Students[0].SleepIn.Location);
            //Student s2 = context.Students
            //    .Include(s => s.SleepIn)
            //    .Where(s => s.Id == 2)
            //    .Single();
            // Console.WriteLine(s2.SleepIn);
            //Console.WriteLine(s2.SleepIn.Location);




















            //var db = context.Database;
            //db.EnsureDeleted();
            //db.EnsureCreated();



            //Student zl = new Student { Name = "zl" };
            //Student lzb = new Student { Name = "lzb" };
            //Student gty = new Student { Name = "gty" };
            //Student lw = new Student { Name = "lw" };

            //Classroom csharp = new Classroom { Name = "csharp" };
            //Classroom sql = new Classroom { Name = "sql" };

            //csharp.Students = new List<Student> { lzb, zl };
            //sql.Students = new List<Student> { zl, gty, lw };

            //Teacher fg = new Teacher { Name = "fg" };
            //Teacher xy = new Teacher { Name = "xy" };
            //fg.Students = new List<Student> { lzb, zl, gty };
            //xy.Students = new List<Student> { gty, lw };

            //Bed closure = new Bed { Location = "闭包间" };
            //Bed factory = new Bed { Location = "抽象工厂" };

            //zl.SleepIn = closure;
            //lw.SleepIn = factory;


            //context.AddRange(fg, xy);
            //context.AddRange(csharp, sql);
            //context.SaveChanges();

            ////Person hyl =
            ////    context.Students.
            ////    Where(s => s.Name == "何奕霖").
            ////    SingleOrDefault();
            //Person hyl =
            //    context.Persons.
            //     //Where(p => p.Name.StartsWith("")).
            //     Where(s => s.Name == "何奕霖").
            //    SingleOrDefault();
            //Student s = (Student)hyl;
            //Console.WriteLine(s.Score);
        }





    }

}